package com.coy.web.dao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.coy.web.common.dto.DataResult;
import com.coy.web.common.dto.PageResult;
import com.coy.web.common.util.security.MD5;
import com.coy.web.feign.dto.UpdatePwdReq;
import com.coy.web.zax.feign.dto.UserInfoDTO;
import com.coy.web.zax.feign.dto.UserInfoQueryDTO;
import com.coy.web.dao.mapper.UserInfoMapper;
import com.coy.web.dao.entity.UserInfo;
import com.coy.web.dao.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户 服务实现类
 *
 * @author chenck
 * @gen 2021-06-12 21:09:08
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    @Override
    public PageResult<UserInfoDTO> queryList(UserInfoQueryDTO param) {
        // 分页查询设置
        Page page = PageHelper.startPage(param.getPageNum().intValue(), param.getPageSize().intValue());
        page.setCount(param.getPageNum() <= 1);

        List<UserInfo> list = this.baseMapper.queryList(param);
        List<UserInfoDTO> dtoList = new ArrayList<>();
        list.forEach(entity -> {
            UserInfoDTO entityDTO = new UserInfoDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            dtoList.add(entityDTO);
        });
        return PageResult.succ(dtoList, Long.valueOf(page.getPageNum()), Long.valueOf(page.getPageSize()), page.getTotal(), Long.valueOf(page.getPages()));
    }

    @Override
    public DataResult<UserInfoDTO> queryUser(String phone) {
        UserInfo userInfo = this.baseMapper.selectOne(Wrappers.<UserInfo>lambdaQuery()
                .eq(UserInfo::getPhone, phone));

        if (null == userInfo) {
            return DataResult.succ();
        }

        UserInfoDTO entityDTO = new UserInfoDTO();
        BeanUtils.copyProperties(userInfo, entityDTO);
        return DataResult.succ(entityDTO);
    }

    @Override
    public DataResult updatePwd(UpdatePwdReq req) {
        UserInfo userInfo = this.baseMapper.selectOne(Wrappers.<UserInfo>lambdaQuery()
                .eq(UserInfo::getPhone, req.getPhone()));

        if (null == userInfo) {
            return DataResult.error("修改密码失败，用户不存在");
        }

        // 当旧密码不为空时，校验旧密码的正确性
        if (StringUtils.isNotBlank(req.getOldPwd())) {
            String oldPwd = MD5.encode(req.getOldPwd());
            if (!oldPwd.equals(userInfo.getPassword())) {
                return DataResult.error("修改密码失败，旧密码错误");
            }
        }

        UserInfo updateUser = new UserInfo();
        updateUser.setId(userInfo.getId());
        updateUser.setPassword(MD5.encode(req.getNewPwd()));
        int rslt = this.baseMapper.updateById(updateUser);
        if (rslt > 0) {
            return DataResult.succ();
        }

        return DataResult.error("修改密码失败");
    }
}
